/////////////////////////////////////////////////////////////////////
//
//  Tackle - A Scrum Tracking Web Site Solution
//  Released under Microsoft Permissive License (Ms-PL)
//  http://www.codeplex.com/Tackle/Project/License.aspx
//
/////////////////////////////////////////////////////////////////////

//
// Original Source from 
// http://tuldoklambat.blogspot.com/2005/11/viewstate-compression-and-_113274519039560470.html
// 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;

public class GZip
{
    public static byte[] Compress(byte[] b)
    {
        MemoryStream ms = new MemoryStream();
        GZipStream zs = new GZipStream(ms, CompressionMode.Compress, true);
        zs.Write(b, 0, b.Length);
        zs.Close();
        return ms.ToArray();
    }

    public static byte[] Decompress(byte[] b)
    {
        MemoryStream ms = new MemoryStream();
        GZipStream zs = new GZipStream(new MemoryStream(b), CompressionMode.Decompress, true);
        byte[] buffer = new byte[4096];
        int size;
        while (true)
        {
            size = zs.Read(buffer, 0, buffer.Length);
            if (size > 0) ms.Write(buffer, 0, size);
            else break;
        }
        zs.Close();
        return ms.ToArray();
    }
}

public class Deflate
{
    public static byte[] Compress(byte[] b)
    {
        MemoryStream ms = new MemoryStream();
        DeflateStream zs = new DeflateStream(ms, CompressionMode.Compress, true);
        zs.Write(b, 0, b.Length);
        zs.Close();
        return ms.ToArray();
    }

    public static byte[] Decompress(byte[] b)
    {
        MemoryStream ms = new MemoryStream();
        DeflateStream zs = new DeflateStream(new MemoryStream(b), CompressionMode.Decompress, true);
        byte[] buffer = new byte[4096];
        int size;
        while (true)
        {
            size = zs.Read(buffer, 0, buffer.Length);
            if (size > 0) ms.Write(buffer, 0, size);
            else break;
        }
        zs.Close();
        return ms.ToArray();
    }
}
